﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FinalProjectLogicLayer
{
    public class Dictionary
    {
        private string sourceFileName;  // name of source file
        private string new_word;            // new randomly selected word
        private bool valid = false;
        private int dictionaryAlphaBetSize = 256;


        public Dictionary(int size, int word_Size, string path)
        {
            this.DictionarySize = size;
            this.wordSize = word_Size;
            this.sourceFileName = path;
            
        }
        /// contructor
        /// </summary>
        public Dictionary()
        {
            this.DictionarySize = 0;
            this.wordSize = 0;
            this.sourceFileName = "";
            
        }
        private int dictionarySize;
        public int DictionarySize
        {
            get
            {
                return dictionarySize;
            }
            set
            {
                if (dictionarySize != value)
                {
                    dictionarySize = value;
                    valid = false;
                }
            }
        }

        private int wordSize;
        public int WordSize
        {
            get
            {
                return wordSize;
            }
            set
            {
                if (value < 1)
                    value = 1;
                if (wordSize != value)
                {
                    wordSize = value;
                    valid = false;
                }
            }
        }
        private string[] dictionaryWords;
        public string[] DictionaryWords
        {
            get
            {
                if (valid == false)
                    return makeDictionary();
                else return dictionaryWords;
            }

            set
            {
                dictionaryWords = value;
                if (value != null)
                {
                    if (value.Length > 0 && value[0].Length > 0)
                    {
                        dictionarySize = value.Length;
                        wordSize = value[0].Length;
                        valid = true;
                    }
                }
                else
                {
                    dictionarySize = 0;
                    wordSize = 0;
                    valid = false;
                }
            }
        }

        public string SourceFileName
        {
           get
           {
               return sourceFileName;
           }
           set
           {
               if (sourceFileName != value)
               {
                   sourceFileName = value;
                   valid = false;
               }
           }
        }
        public int DictionaryAlphaBetSize
        {
            get { return dictionaryAlphaBetSize; }
            set 
            {
                if (dictionaryAlphaBetSize != value)
                {
                    dictionaryAlphaBetSize = value;
                    valid = false;
                }
            }
        }
        /// <summary>
        /// check if selected word is already in dictionary
        /// return true if yes 
        /// return false if not 
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        private bool checkWord(string word, string[] words, int index)
        {
            if (dictionaryWords == null) return false;
            for (int i = 0; i < index; i++)
                if (words[i].Equals(word)) return true;
            return false;
        }
        private string[] makeDictionary()
        {
            if (SourceFileName == "") return null; //if no source file is selected
            int counter=0;                  // counter for how many failed attempts made to make new word
            Random random = new Random();   //a random variable for index choosings
            String fileContents = FeatureExtraction.fileToString(this.sourceFileName);
            int randomNumber; 
            dictionaryWords = new string[dictionarySize];
            for (int i = 0; i < dictionarySize; i++)
                {
                    randomNumber = random.Next(0, fileContents.Length - wordSize);
                    new_word = fileContents.Substring(randomNumber, wordSize);
                    if (checkWord(new_word, dictionaryWords, i) == false)   // checking if word is ok
                    {
                        dictionaryWords[i] = new_word;
                    }
                    else
                        {
                            if (counter > 200)
                            {
                                return null;
                            }
                            i--;                              // incrementing counter
                            counter++;
                        }
   
                }
            valid = true;
            return dictionaryWords;
        }

        /// <summary>
        /// Dictionary serial number, used to identefy the dictionary from others
        /// </summary>
        private String id="";

        public String ID
        {
            get {
                id = "";
                if (DictionaryWords!=null)
                    foreach (String word in dictionaryWords)
                    {
                        id += word + ",";
                    }
                return id; 
                }
            set { /*NO SET OPTION*/ }
        }

    }
}
